package com.ruoyi.job.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.ruoyi.job.domain.SysJobCode;
import com.ruoyi.job.domain.TCwCheckBatch;
import com.ruoyi.job.domain.TCwPayOrder;
import com.ruoyi.job.feign.ChechBatchFeignService;
import com.ruoyi.job.feign.PayOrderFeignService;
import com.ruoyi.job.service.SysJobCodeService;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.GroovyShell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务跑批
 *
 * @author rinyea
 */
@Component("moneyTask")
public class MoneyTask {

    @Autowired
    private PayOrderFeignService payOrderFeignService;

    @Autowired
    private ChechBatchFeignService chechBatchFeignService;

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private SysJobCodeService codeService;

    public void dongtai(Integer integer) throws InstantiationException, IllegalAccessException {
        System.out.println("===qroovy执行代码===");
        GroovyShell groovyShell = new GroovyShell();
        String s = "int i = 10;\n" +
                "int j = 2;\n" +
                "int k = i*j;\n" +
                "System.out.println(\"Result is\" + k);";
        groovyShell.evaluate(s);
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        SysJobCode byId = codeService.getById(integer);
        System.out.println("===数据库中的===");
        GroovyShell shell = new GroovyShell();
        shell.evaluate(byId.getCodes());

//        Class aClass = groovyClassLoader.parseClass("import com.ruoyi.job.domain.TCwPayOrder;" +
//                "import cn.hutool.core.date.DateUtil;" +
//                "import com.ruoyi.job.feign.PayOrderFeignService;" +
//                "import org.springframework.beans.factory.annotation.Autowired;" +
//                "import org.springframework.stereotype.Component;\n" +
//                "@Component\n" +
//                "public class fsad {\n" +
//                "   @Autowired\n" +
//                "PayOrderFeignService payOrderFeignService;" +
//                "public void cha(String abc){" +
//                "       Date date = new Date();\n" +
//                "        TCwPayOrder tCwPayOrder = new TCwPayOrder();\n" +
//                "        tCwPayOrder.setBeginTime(DateUtil.beginOfDay(DateUtil.offsetDay(date,-1)));\n" +
//                "        tCwPayOrder.setEndTime(DateUtil.beginOfDay(date));\n" +
//                "        System.out.println(tCwPayOrder);" +
//                "List<TCwPayOrder> tCwPayOrders = payOrderFeignService.dateList(tCwPayOrder);\n" +
//                "        System.out.println(tCwPayOrders);" +
//                "   }" +
//                "}");


//        groovyShell.evaluate("import com.ruoyi.job.domain.TCwPayOrder;" +
//                "import cn.hutool.core.date.DateUtil;" +
//                "import com.ruoyi.job.feign.PayOrderFeignService;" +
//                "import org.springframework.beans.factory.annotation.Autowired;" +
//                "import java.util.List;" +
//                "public class fsad {\n" +
//                "   @Autowired\n" +
//                "PayOrderFeignService payOrderFeignService;" +
//                "public void cha(){" +
//                "       Date date = new Date();\n" +
//                "        TCwPayOrder tCwPayOrder = new TCwPayOrder();\n" +
//                "        tCwPayOrder.setBeginTime(DateUtil.beginOfDay(DateUtil.offsetDay(date,-1)));\n" +
//                "        tCwPayOrder.setEndTime(DateUtil.beginOfDay(date));\n" +
//                "        System.out.println(tCwPayOrder);" +
//                "List<TCwPayOrder> tCwPayOrders = payOrderFeignService.dateList(tCwPayOrder);\n" +
//                "        System.out.println(tCwPayOrders);" +
//                "   }" +
//                "}");
    }

    public void duizhang() throws ParseException, AlipayApiException {
        System.out.println("=run=定=时=任=务=");

        Date date = new Date();

        //订单们
        TCwPayOrder tCwPayOrder = new TCwPayOrder();
//        tCwPayOrder.setBeginTime(DateUtil.beginOfDay(date));
//        tCwPayOrder.setEndTime(DateUtil.beginOfDay(DateUtil.offsetDay(date,1)));
        tCwPayOrder.setBeginTime(DateUtil.beginOfDay(DateUtil.offsetDay(date,-1)));
        tCwPayOrder.setEndTime(DateUtil.beginOfDay(date));
        System.out.println(tCwPayOrder);
        List<TCwPayOrder> tCwPayOrders = payOrderFeignService.dateList(tCwPayOrder);

        System.out.println(tCwPayOrders);

        //
        //List<TCwPayOrder> collect = tCwPayOrders.stream().filter(t -> ObjectUtil.isNotNull(t.getPayAmount())).collect(Collectors.toList());

        //
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(date);

        String yesterday = simpleDateFormat.format(DateUtil.yesterday());

        TCwCheckBatch tCwCheckBatch = new TCwCheckBatch();
        //tCwCheckBatch.setBillDay(format);
        tCwCheckBatch.setBillDay(yesterday);
        System.out.println(tCwCheckBatch);
        TCwCheckBatch one = chechBatchFeignService.one(tCwCheckBatch);
        System.out.println(one);
        Integer batchNum = 1;
        if(ObjectUtil.isNotNull(one)){
            Long batchNum1 = one.getBatchNum();
            Integer integer = batchNum1.intValue();
            batchNum = batchNum+integer;
        }
        System.out.println(batchNum);
        tCwCheckBatch.setBatchNum(Integer.toUnsignedLong(batchNum));
        tCwCheckBatch.setCheckDay(format);
        tCwCheckBatch.setBatchNo(format+"00"+batchNum);
        Long l = new Long(0);
        tCwCheckBatch.setCreateUserid(l);
        tCwCheckBatch.setUpdateUserid(l);
        Date date1 = new Date();
        tCwCheckBatch.setCreateTime(date1);
        tCwCheckBatch.setUpdateTime(date1);

        Integer diffCount = 0;
        Integer count = 0;

        for(TCwPayOrder payOrder :tCwPayOrders){
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            AlipayTradeQueryModel model = new AlipayTradeQueryModel();
            model.setOutTradeNo(payOrder.getOrderNo());
            request.setBizModel(model);
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            System.out.println(response.getBody());

            if(response.getCode().equals("10000")){
                if(ObjectUtil.isNull(payOrder.getPayAmount())){
                    diffCount++;
                }
            }else if(response.getCode().equals("40004")){
                if(ObjectUtil.isNotNull(payOrder.getPayAmount())){
                    diffCount++;
                }
            }
        }

        tCwCheckBatch.setDiffCount(diffCount.longValue());
        int scadd = chechBatchFeignService.scadd(tCwCheckBatch);
    }
}
